package com.lucien.system.service.impl;

import com.lucien.system.domain.SysGroupMember;
import com.lucien.system.domain.SysGroupMemberExample;
import com.lucien.system.mapper.SysGroupMemberMapper;
import com.lucien.system.service.SysGroupMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysGroupMemberServiceImpl implements SysGroupMemberService {

    @Autowired
    private SysGroupMemberMapper memberMapper;

    /**
     * 通过GroupId获取群聊成员
     * @param groupId
     * @return
     */
    @Override
    public List<SysGroupMember> selectByGroupId(Long groupId) {
        SysGroupMemberExample example = new SysGroupMemberExample();
        example.setOrderByClause("userId asc");
        SysGroupMemberExample.Criteria criteria = example.createCriteria();
        criteria.andGroupidEqualTo(groupId);

        return memberMapper.selectByExample(example);
    }

    /**
     * 通过UserId获取所在群聊ID
     * @param userId
     * @return
     */
    @Override
    public Integer[] selectByUserId(Integer userId) {
        SysGroupMemberExample example = new SysGroupMemberExample();
        example.setOrderByClause("userId asc");
        SysGroupMemberExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(userId);

        List<SysGroupMember> members = memberMapper.selectByExample(example);
        if (members.size() == 0){
            return new Integer[0];
        }else {
            Integer[] result = new Integer[members.size()];
            int i = 0;
            for (SysGroupMember member : members) {
                result[i] = Math.toIntExact(member.getGroupid());
                i++;
            }
            return result;
        }


    }

    /**
     * 返回群聊成员数量
     * @param groupId
     * @return
     */
    @Override
    public int groupLen(Long groupId) {
        SysGroupMemberExample example = new SysGroupMemberExample();
        SysGroupMemberExample.Criteria criteria = example.createCriteria();
        criteria.andGroupidEqualTo(groupId);
        List<SysGroupMember> members = memberMapper.selectByExample(example);

        return members.size();
    }
}
